package com.itrip.itriosearch.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.itrip.itriosearch.entity.Mail;
import com.itrip.itriosearch.entity.User;
import com.itrip.itriosearch.mapper.UserMapper;
import com.itrip.itriosearch.service.UserService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itrip.itriosearch.util.DtoUtil;
import com.itrip.itriosearch.util.MailSendUtil;
import com.itrip.itriosearch.util.RandomUtils;
import com.itrip.itriosearch.util.RedisUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 用户表 服务实现类
 * </p>
 *
 * @author 四阶段项目二组
 * @since 2020-02-25
 */
@Service
public class UserServiceImpl extends ServiceImpl<UserMapper, User> implements UserService {


    @Autowired
    private RedisUtil redisUtil;

    @Override
    public User login(User user) {
        QueryWrapper<User> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_code", user.getUserName()).eq("user_password", user.getUserPassword()).
                ne("activated",0);
        User sysuser = baseMapper.selectOne(queryWrapper);
        if (sysuser != null) {
            return sysuser;
        }
        return null;
    }

    @Override
    public User findByUsername(String name) {
        return baseMapper.getUserByName(name);
    }

    @Override
    public synchronized boolean insertUser(User user) {
        //判断用户是否存在
        User userCode = baseMapper.getUserByName(user.getUserCode());
        if (userCode == null) {
            //存入数据到数据库
            int id = baseMapper.insertUse(user);
            System.out.println(user.getId() + "你");
            if (id > 0) {
                MailSendUtil mailSendUtil = new MailSendUtil();
                Mail mail = new Mail();
                //生成4位随机数
                String VerificationCode = RandomUtils.generateNumberString(4);
                //mailSendUtil.TheSender = user.getUserCode();
                //存入发送邮箱的信息
                mail.setMail(user.getUserCode());
                mail.setTheme("激活验证码");
                mail.setTemplate("您好本次验证码为:" + VerificationCode + ",有效时间为10分钟。");
                try {
                    //执行发送
                    mailSendUtil.sendMail(mail);
                    //存入验证码到缓存中
                    redisUtil.set(user.getId().toString(), VerificationCode, 600);
                    return true;
                } catch (Exception e) {
                    e.printStackTrace();
                    return false;
                }

            } else {
                return false;
            }

        } else {
            return false;
        }
    }

    @Override
    public synchronized Integer  emailTheActivation(String user, String code) {
        //判断是否有该用户存在
        User userCode = baseMapper.getUserByName(user);
        if (userCode != null) {
            //缓存中获取邮箱的验证码
            String VerificationCode = (String) redisUtil.get(userCode.getId().toString());
            //判断从缓存中获取的验证码是否与输入的匹配(匹配返回1，用户不存在返回2，验证码错误返回3)
            if (code.equals(VerificationCode)) {
                //验证码匹配后修改激活信息
                baseMapper.update(1,userCode.getId());
                //修改激活信息后台，删除缓存中的验证码
                redisUtil.del(userCode.getId().toString());
                return 1;
            } else {
                return 3;
            }
        } else {
            return 2;
        }

    }


}

